package com.asia5b.a5mall.member.activity

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.member.adapter.PointListAdapter
import com.asia5b.a5mall.member.databinding.ActivityPointlistBinding
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.logic.viewmodel.member.PointListActivityVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.member.PointModel
import com.asia5b.a5mall.middleware.utils.newDivider
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.activity_pointlist.*

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   会员积分页面
 */
@Route(path = ActCons.PointListActivity)
class PointListActivity : SimpleActivity<PointListActivityVM, ActivityPointlistBinding>() {

    override val viewModelClazz: Class<PointListActivityVM>
        get() = PointListActivityVM::class.java

    override fun getDataBinding(): ActivityPointlistBinding {
        return ActivityPointlistBinding.inflate(layoutInflater)
    }

    override var sIsDark = true
    val mRecyclerView by lazy { rv_pointlist }
    val mDatas = arrayListOf<PointModel>()
    val mAdapter by lazy { PointListAdapter(mDatas) }


    override fun initView() {
        mSmartRefreshLayout=mDataBinding.autoPointlist
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
            addItemDecoration(newDivider(mActivity))
        }
        getDatas(0)
    }

    override fun initEvent() {
    }

  override  fun startObserver()
    {
        mViewModel.getDatasResultLiveData.observe(this, Observer {
            getDatasResult(it)
        })
    }

    override fun initData() {

    }

    fun getDatas(refresh: Int) {
        mViewModel.getDatas(refresh)

    }

    fun getDatasResult(resultModel: ResultModel<ArrayList<PointModel>> ) {
        if (resultModel.page <= 1) {
            mDatas.clear()
        }
        if (!resultModel.data.isNullOrEmpty()) {
            mAdapter.addData(resultModel.data!!)
        }
        mDataBinding.autoPointlist.setEnableLoadMore(resultModel.page < resultModel.sumPage)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        getDatas(2)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        getDatas(1)
    }
}